package pimp.organizermanager;

import java.util.*;

public class Month {
    private HashMap<Integer, Day> days;
    private List<Event> events;
    private int thisMonth;
    private int thisYear;
    private int daysThisMonth;

    public Month(int month, int year) {
        days = new HashMap<Integer, Day>();
        events = new ArrayList<Event>();
        thisMonth = month;
        thisYear = year;

        daysThisMonth = StandardCalendar.calculateDaysInMonth(thisYear, thisMonth);
    }

    public boolean addEvent(Event event, Date beginDate, Date endDate) {
        int beginYear = beginDate.getThisYear();
        int endYear = endDate.getThisYear();
        int beginMonth = beginDate.getThisMonth();
        int endMonth = endDate.getThisMonth();
        int beginDay = beginDate.getThisDay();
        int endDay = endDate.getThisDay();

        if(beginDay == 0 && endDay == 0) {
            events.add(event);
            event.addMonth(this);
        }
        else {
            if(beginYear == endYear) {
                if(beginMonth == endMonth) {
                    for(int i = beginDay; i <= endDay; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                }
                else if(thisMonth == beginMonth) {
                    for(int i = beginDay; i <= daysThisMonth; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                }
                else if(thisMonth == endMonth) {
                    for(int i = 1; i <= endDay; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                }
                else {
                    for(int i = 1; i <= daysThisMonth; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                }
            }
            else {
                if(thisYear == beginYear) {
                    if(thisMonth == beginMonth) {
                        for(int i = beginDay; i <= daysThisMonth; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                    }
                    else {
                        for(int i = 1; i <= daysThisMonth; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                    }
                }
                else if(thisYear == endYear) {
                    if(thisMonth == endMonth) {
                        for(int i = 1; i <= endDay; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                    }
                    else {
                        for(int i = 1; i <= daysThisMonth; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                    }
                }
                else {
                    for(int i = 1; i <= daysThisMonth; ++i) {
                        Day day;
                        if(days.get(i) == null) {
                            days.put(i, new Day(i));
                        }
                        day = days.get(i);
                        if(day.addEvent(event) == false) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

    public Map<ID, GlobalEvent> getGlobalEventByDate(Date beginDate, Date endDate,
            Map<ID, GlobalEvent> map) {
        int beginYear = beginDate.getThisYear();
        int endYear = endDate.getThisYear();
        int beginMonth = (beginDate.getThisMonth()) % 13;
        int endMonth = (endDate.getThisMonth()) % 13;
        int beginDay = beginDate.getThisDay();
        int endDay = endDate.getThisDay();
        if(beginYear != endYear) {
            if(thisYear == beginYear) {
                if(thisMonth == beginMonth) {
                    for(int i = beginDay; i <= daysThisMonth; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                            day.getGlobalEvent(map);
                        }
                    }
                }
                else {
                    for(int i = 1; i <= daysThisMonth; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                           day.getGlobalEvent(map);
                        }
                    }
                }
            }
            else if (thisYear == endYear) {
                if(thisMonth == endMonth) {
                    for(int i = 1; i <= endDay; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                            day.getGlobalEvent(map);
                        }
                    }
                }
                else {
                    for(int i = 1; i <= daysThisMonth; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                           day.getGlobalEvent(map);
                        }
                    }
                }
            }
            else {
                for(int i = 1; i <= daysThisMonth; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                           day.getGlobalEvent(map);
                        }
                }
            }
        }
        else {
            if(thisMonth == beginMonth && thisMonth == endMonth) {
                for(int i = beginDay; i <= endDay; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                           day.getGlobalEvent(map);
                        }
                    }
            }
            else if(thisMonth == beginMonth) {
                for(int i = beginDay; i <= daysThisMonth; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                           day.getGlobalEvent(map);
                        }
                    }
            }
            else if(thisMonth == endMonth) {
                for(int i = 1; i <= endDay; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                           day.getGlobalEvent(map);
                        }
                    }
            }
            else {
                for(int i = 1; i <= daysThisMonth; ++i) {
                        Day day = days.get(i);
                        if(day != null) {
                           day.getGlobalEvent(map);
                        }
                    }
            }
        }
        for(int i = 0; i < events.size(); ++i) {
            Event event = events.get(i);
            map.put(event.getId(), event);
        }
        return map;
    }
    public String toString()
    {
        String out = new String();
        return out;
    }
}
